﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace CMS
{
    public partial class log : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bt_login_Click(object sender, ImageClickEventArgs e)
        {   
            //判断验证码
            string checkcode = "";
            //生成的验证码被保存到session中
            if (Session["CheckCode"] != null)
            {
                checkcode = Session["CheckCode"].ToString();
                if (this.checkCode.Text != checkcode)
                {
                    Response.Write("<script type='text/javascript'>alert('验证码有误！')</script>");
                    if (Tea.Checked) {
                        this.student.Style["display"] = "none";
                        this.teacher.Style["display"] = "block";
                        this.admin.Style["display"] = "none";
                    }
                    else if (Adm.Checked) {
                        this.student.Style["display"] = "none";
                        this.teacher.Style["display"] = "none";
                        this.admin.Style["display"] = "display";
                    }
                    return;
                }

            }

            string sql = "";
            SqlHelper sqlHelper = null;
            SqlParameter[] pms = null;
            SqlDataReader dr = null;

            //登录
            if (Stu.Checked) {
                sqlHelper = new SqlHelper();
                sql = "SELECT   login_Stu_pwd,login_Stu_name FROM      login_Stu WHERE   (login_Stu_user = @stu_user)";
                pms = new SqlParameter[1];
                pms[0] = new SqlParameter("@stu_user", stu_user.Text);
                dr = sqlHelper.ExecuteReader(sql, pms);
                if (dr.Read() && dr["login_Stu_pwd"].ToString() == this.stu_pwd.Text)
                {
                    Session["stu_name"] = dr["login_Stu_name"].ToString();
                    Session["stu_no"] = stu_user.Text;
                    //Response.Redirect("index.aspx");
                    Response.Write("<script>alert('登录成功');location.href='index.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('登录失败')</script>");
                    this.student.Style["display"] = "block";              
                    this.teacher.Style["display"] = "none";
                    this.admin.Style["display"] = "none";
                }
            }
            else if (Tea.Checked) {
                sqlHelper = new SqlHelper();
                sql = "SELECT   login_Tea_pwd,login_Tea_name FROM      login_Tea WHERE   (login_Tea_user = @tea_user)";
                pms = new SqlParameter[1];
                pms[0] = new SqlParameter("@tea_user", tea_user.Text);
                dr = sqlHelper.ExecuteReader(sql, pms);
                if (dr.Read() && dr["login_Tea_pwd"].ToString() == this.tea_pwd.Text)
                {
                    Session["tea_name"] = dr["login_Tea_name"].ToString();
                    Session["tea_no"] = tea_user.Text;
                    //Response.Redirect("index.aspx");
                    Response.Write("<script>alert('登录成功');location.href='index1.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('登录失败')</script>");
                    this.student.Style["display"] = "none";
                    this.teacher.Style["display"] = "block";
                    this.admin.Style["display"] = "none";
                }
            }
            else if (Adm.Checked) {
                sqlHelper = new SqlHelper();
                sql = "SELECT   login_Adm_pwd FROM      login_Adm WHERE   (login_Adm_user = @admin_user)";
                pms = new SqlParameter[1];
                pms[0] = new SqlParameter("@admin_user", admin_user.Text);
                dr = sqlHelper.ExecuteReader(sql, pms);
                if (dr.Read() && dr["login_Adm_pwd"].ToString() == this.admin_pwd.Text)
                {
                    Response.Redirect(admin_user.Text + ".aspx?user=" + admin_user.Text);
                }
                else
                {
                    Response.Write("<script>alert('登录失败')</script>");
                    this.student.Style["display"] = "none";
                    this.teacher.Style["display"] = "none";
                    this.admin.Style["display"] = "display";
                }
            }
            
      
        }

        protected void bt_reset_Click(object sender, ImageClickEventArgs e)
        {
            stu_pwd.Text = "";
            stu_user.Text = "";
            checkCode.Text = "";
            tea_user.Text = "";
            tea_pwd.Text = "";
            admin_user.Text = "";
            admin_pwd.Text = "";
        }

    }
}